package com.chtw.oasystem.service.impl;

import com.chtw.oasystem.bean.FullUserInfo;
import com.chtw.oasystem.bean.Role;
import com.chtw.oasystem.bean.RoleExample;
import com.chtw.oasystem.common.changelogs.ChangeLogsUtils;
import com.chtw.oasystem.mapper.RoleMapper;
import com.chtw.oasystem.service.RoleService;
import com.chtw.oasystem.utils.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author chtw
 * @date 2020-11-17 22:58
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private ChangeLogServiceImpl changeLogService;

    @Override
    public boolean insertRole(HttpSession session, Role role) {
        try {
            int created = roleMapper.insert(role);
            FullUserInfo fullUserInfo = Session.getCurrentUser(session);
            if (created > 0 && fullUserInfo != null){
                changeLogService.saveChangeLog(ChangeLogsUtils.generateRoleChangeLog(fullUserInfo.getUserName(), role.getRoleName(), "添加"));
                return true;
            }
            return  false;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<Role> list() {
        return roleMapper.selectByExample(new RoleExample());
    }
}
